package cn.lingyangwl.agile.pay.common.service.impl;

import cn.lingyangwl.agile.pay.common.assembly.PayInterfaceAssembly;
import cn.lingyangwl.framework.core.utils.spring.SpringUtils;
import cn.lingyangwl.framework.mybatis.mate.annotations.FieldUniqueCheck;
import cn.lingyangwl.framework.mybatis.mate.enums.OperationTypeEnum;
import cn.lingyangwl.framework.mybatis.plus.utils.PageUtil;
import cn.lingyangwl.framework.tool.core.StringUtils;
import cn.lingyangwl.framework.tool.core.exception.BizException;
import cn.lingyangwl.agile.common.core.exception.enums.GlobalErrorEnum;
import cn.lingyangwl.agile.pay.model.entity.PayInterfaceConfig;
import cn.lingyangwl.agile.pay.model.entity.PayInterfaceDefine;
import cn.lingyangwl.agile.pay.model.entity.PayOrder;
import cn.lingyangwl.agile.pay.model.rqrs.PayIfDefineQL;
import cn.lingyangwl.agile.pay.model.rqrs.PayInterfaceDefineRQ;
import cn.lingyangwl.agile.pay.common.manager.PayInterfaceConfigManager;
import cn.lingyangwl.agile.pay.common.manager.PayMqSender;
import cn.lingyangwl.agile.pay.common.mapper.PayInterfaceDefineMapper;
import cn.lingyangwl.agile.pay.common.service.PayInterfaceDefineService;
import cn.lingyangwl.agile.pay.common.service.PayOrderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shenguangyang
 */
@Service
public class PayInterfaceDefineServiceImpl extends ServiceImpl<PayInterfaceDefineMapper, PayInterfaceDefine>
        implements PayInterfaceDefineService {
    @Resource
    private PayInterfaceAssembly payInterfaceAssembly;
    @Resource
    private PayInterfaceConfigManager payInterfaceConfigManager;
    @Resource
    private PayOrderService payOrderService;
    @Resource
    private PayMqSender payMqSender;

    @Override
    public IPage<PayInterfaceDefine> pagePayIfDefine(PayIfDefineQL query) {
        IPage<PayInterfaceDefine> page = PageUtil.getPage();
        LambdaQueryWrapper<PayInterfaceDefine> lqw = PayInterfaceDefine.lqw()
                .like(StringUtils.isNotEmpty(query.getIfCode()), PayInterfaceDefine::getIfCode, query.getIfCode())
                .like(StringUtils.isNotEmpty(query.getIfName()), PayInterfaceDefine::getIfName, query.getIfName())
                .eq(StringUtils.isNotEmpty(query.getStatus()), PayInterfaceDefine::getStatus, query.getStatus())
                .orderByDesc(PayInterfaceDefine::getCreatedAt);
        return this.page(page, lqw);
    }

    @Override
    public void savePayIfDefine(PayInterfaceDefineRQ req) {
        PayInterfaceDefine entity = payInterfaceAssembly.toPayInterfaceDefine(req);
        SpringUtils.getAopProxy(this).savePayIfDefine(entity);
    }

    @FieldUniqueCheck(type = OperationTypeEnum.SAVE)
    public void savePayIfDefine(PayInterfaceDefine entity) {
        if (!this.save(entity)) {
            throw new BizException(GlobalErrorEnum.OPERATION_FAIL_CREATE);
        }
    }

    @Override
    public void updatePayIfDefine(PayInterfaceDefineRQ req) {
        req.setIfCode(null);
        PayInterfaceDefine entity = payInterfaceAssembly.toPayInterfaceDefine(req);
        SpringUtils.getAopProxy(this).updatePayIfDefine(entity);
    }

    @FieldUniqueCheck(type = OperationTypeEnum.UPDATE)
    public void updatePayIfDefine(PayInterfaceDefine entity) {
        if (!this.updateById(entity)) {
            throw new BizException(GlobalErrorEnum.OPERATION_FAIL_UPDATE);
        }
    }

    @Override
    public PayInterfaceDefine getByIfCode(String ifCode) {
        LambdaQueryWrapper<PayInterfaceDefine> lqw = PayInterfaceDefine.lqw()
                .eq(PayInterfaceDefine::getIfCode, ifCode);
        return this.getOne(lqw);
    }

    @Override
    public void deletePayIfDefine(Long id) {
        PayInterfaceDefine define = this.getById(id);
        // 校验该支付方式是否有服务商或商户配置参数或者已有订单
        if (payInterfaceConfigManager.count(PayInterfaceConfig.lqw().eq(PayInterfaceConfig::getIfCode, define.getIfCode())) > 0
                || payOrderService.count(PayOrder.lqw().eq(PayOrder::getIfCode, define.getIfCode())) > 0) {
            throw new BizException("该支付接口已有服务商或商户配置参数或已发生交易，无法删除！");
        }

        boolean result = this.removeById(id);
        if (!result) {
            throw new BizException(GlobalErrorEnum.OPERATION_FAIL_DELETE);
        }
    }

    @Override
    public List<PayInterfaceDefine> listPayIfDefine(PayIfDefineQL query) {
        LambdaQueryWrapper<PayInterfaceDefine> lqw = PayInterfaceDefine.lqw()
                .like(StringUtils.isNotEmpty(query.getIfCode()), PayInterfaceDefine::getIfCode, query.getIfCode())
                .like(StringUtils.isNotEmpty(query.getIfName()), PayInterfaceDefine::getIfName, query.getIfName())
                .eq(StringUtils.isNotEmpty(query.getStatus()), PayInterfaceDefine::getStatus, query.getStatus());
        return this.list(lqw); 
    }
}
